//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.ObjectModel;

namespace OpenLS.Chart.UI
{
    class ChartToChartInfoConverter
    {
        public static ChartLayout GetInfo (ChartBase chart)
        {
            if (chart == null)
                return null;
            if (chart is AreaChartBase)
                return GetAreaInfo((AreaChartBase) chart);
            if (chart is LineChartBase)
                return GetLineInfo((LineChartBase) chart);
            if (chart is BarChartBase)
                return GetBarInfo((BarChartBase) chart);
            if (chart is PieChartBase)
                return GetPieInfo((PieChartBase) chart);
            if (chart is ScatterChart)
                return GetScatterInfo((ScatterChart) chart);
            throw new NotSupportedException();
        }

        private static ChartLayout GetScatterInfo(ScatterChart scatterChart)
        {
            return new ScatterChartLayout(scatterChart);
        }

        private static ChartLayout GetPieInfo(PieChartBase pieChartBase)
        {
            if (pieChartBase.Series.Count == 0)
                return null;
            return new PieChartLayout(pieChartBase.Series[0]);
        }

        private static ChartLayout GetBarInfo(BarChartBase barChartBase)
        {
            switch (barChartBase.Grouping)
            {
                case BarGrouping.Standard:
                case BarGrouping.Clustered:
                    return new ClusteredChartLayout(barChartBase.Series, true);
                case BarGrouping.Stacked:
                    return new StackedChartLayout<BarStack>(barChartBase.Series);
                case BarGrouping.PercentStacked:
                    return new PercentStackedChartLayout<BarStack>(barChartBase.Series);
                default:
                    throw new NotSupportedException();
            }

        }

        private static ChartLayout GetLineInfo(LineChartBase lineChartBase)
        {
            switch (lineChartBase.Grouping)
            {
                case Grouping.Standard:
                    return new ClusteredChartLayout(lineChartBase.Series, false);
                case Grouping.Stacked:
                    return new StackedChartLayout<NonBarStack>(lineChartBase.Series);
                case Grouping.PercentStacked:
                    return new PercentStackedChartLayout<NonBarStack>(lineChartBase.Series);
                default:
                    throw new NotSupportedException();
            }

        }

        private static ChartLayout GetAreaInfo(AreaChartBase areaChartBase)
        {
            switch (areaChartBase.Grouping)
            {
                case Grouping.Standard:
                    return new StandardChartLayout(areaChartBase.Series, true);
                case Grouping.Stacked:
                    return new StackedChartLayout<NonBarStack>(areaChartBase.Series);
                case Grouping.PercentStacked:
                    return new PercentStackedChartLayout<NonBarStack>(areaChartBase.Series);
                default:
                    throw new NotSupportedException();
            }

        }
        public static ChartLayout GetLayout<T>(Grouping grouping, ObservableCollection<T> series)
        {
            switch (grouping)
            {
                case Grouping.Standard:
                    return new StandardChartLayout(series, true);
                case Grouping.Stacked:
                    return new StackedChartLayout<NonBarStack>(series);
                case Grouping.PercentStacked:
                    return new PercentStackedChartLayout<NonBarStack>(series);
                default:
                    throw new NotSupportedException();
            }

        }
        public static ChartLayout GetLayout<T>(BarGrouping grouping, ObservableCollection<T> series)
        {
            switch (grouping)
            {
                case BarGrouping.Standard:
                    return new StandardChartLayout(series, true);
                case BarGrouping.Clustered:
                    return new ClusteredChartLayout(series, true);
                case BarGrouping.Stacked:
                    return new StackedChartLayout<BarStack>(series);
                case BarGrouping.PercentStacked:
                    return new PercentStackedChartLayout<BarStack>(series);
                default:
                    throw new NotSupportedException();
            }

        }
    }
}
